<template>
  <!-- 逻辑：在这个页面获取数据，通过props传参的方式给各个组件传参 -->
  <div class="report">
    <tableTool>
      <div slot="export">
        <el-button type="primary" @click="excelExport">导出</el-button>
      </div>
      <div class="tool-form" slot="form">
        <div class="input-group">
          <span class="lable">设备名称：</span>
          <el-input placeholder="请输入" v-model="filterEquipmentName" />
        </div>
        <div class="input-group">
          <span class="lable">设备类别：</span>
          <el-input placeholder="请输入" v-model="filterCategoryName" />
        </div>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="success" @click="onReset">重置</el-button>
      </div>
    </tableTool>
    <customTable :list="list" :combined="combined">
      <template v-slot:title>
        <tr>
          <th colspan="15">{{ orgName }}</th>
        </tr>
        <tr>
          <th>设备编号</th>
          <th>设备名称</th>
          <th>规格型号</th>
          <th>数量</th>
          <th>车牌号</th>
          <th>生产厂商</th>
          <th>产品合格证号</th>
          <th>设备来源</th>
          <th>进场日期</th>
          <th>出场日期</th>
          <th>操作人员</th>
        </tr>
      </template>
      <template #body="{ list }">
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.equipmentNumber }}</td>
          <td>{{ item.equipmentName }}</td>
          <td>{{ item.equipmentStandard }}</td>
          <td>{{ item.quantity }}</td>
          <td>{{ item.carNumber }}</td>
          <td>{{ item.manufacturer }}</td>
          <td>{{ item.qualificationNo }}</td>
          <td>{{ item.equipmentSource }}</td>
          <td>{{ item.mobilizationTime }}</td>
          <td>{{ item.exitTime }}</td>
          <td>{{ item.operator }}</td>
        </tr>
      </template>
      <template #empty>
        <tr>
          <td colspan="15" class="empty">暂无数据</td>
        </tr>
      </template>
      <template #footer="{ combined }">
        <tr>
          <td>合计</td>
          <td>{{ combined.equipmentName }}</td>
          <td>{{ combined.equipmentStandard }}</td>
          <td>{{ combined.quantity }}</td>
          <td>{{ combined.carNumber }}</td>
          <td>{{ combined.manufacturer }}</td>
          <td>{{ combined.qualificationNo }}</td>
          <td>{{ combined.equipmentSource }}</td>
          <td>{{ combined.mobilizationTime }}</td>
          <td>{{ combined.exitTime }}</td>
          <td>{{ combined.operator }}</td>
        </tr>
      </template>
    </customTable>
    <pagination @onCurrentChange="handleCurrentChange" :total="page.total"></pagination>
  </div>
</template>
<script>
import tableTool from '../components/tableTool.vue'
import customTable from '../components/customTable.vue'
import pagination from '../components/pagination.vue'
import request from '@/api/comprehensiveReport/equipmentManagement.js'
import { combinedFn, formatTime } from '@/pages/comprehensiveReport/equipmentManagement/mixin'

export default {
  name: 'specialEquipmentStatisticsTableProject',
  components: { tableTool, customTable, pagination },
  data () {
    return {
      list: [],
      orgName: '',
      page: {
        total: 0,
        currentPage: 1,
        limit: 10,
      },
      combined: {},
      headers: {
        设备编号: 'equipmentNumber',
        设备名称: 'equipmentName',
        规格型号: 'equipmentStandard',
        数量: 'quantity',
        车牌号: 'carNumber',
        生产厂商: 'manufacturer',
        产品合格证号: 'qualificationNo',
        设备来源: 'equipmentSource',
        进场日期: 'mobilizationTime',
        出场日期: 'exitTime',
        操作人员: 'operator',
      },
      filterCategoryName: '',
      filterEquipmentName: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    onSearch () {
      this.getList()
    },
    onReset () {
      this.filterCategoryName = ''
      this.filterEquipmentName = ''
      this.getList()
    },
    async getList () {
      const orgId = this.$route.query.orgId || localStorage.orgId
      this.orgName = this.$route.query.orgName || this.$store.state.userInfo.orgInfo.orgName
      const data = await request.getEquipmentSpecialDetails({
        page: this.page.currentPage,
        limit: this.page.limit,
        orgId,
        filterCategoryName: this.filterCategoryName,
        filterEquipmentName: this.filterEquipmentName
      })
      console.log('发送请求，获取数据', data)
      if (data.list.length == 0) {
        this.list = []
        this.combined = {}
        this.page.total = 0
        return;
      }
      this.page.total = data.totalCount
      const { list, combined } = combinedFn(Object.values(this.headers).slice(1), data.list, ['quantity'])
      this.list = formatTime(['mobilizationTime', 'exitTime'], list)
      this.combined = combined
    },
    handleCurrentChange (currentPage) {
      this.page.currentPage = currentPage
      this.getList()
    },
    excelExport () {
      const multiHeader = [[this.orgName, '', '', '', '', '', '', '', '', '', '']]
      const merges = ['A1:K1']
      import('@/utils/Export2Excel.js').then(async (excel) => {
        const orgId = this.$route.query.orgId || localStorage.orgId
        const res = await request.getEquipmentSpecialDetails({
          page: 1,
          limit: this.page.total,
          orgId,
        })
        const list = formatTime(['mobilizationTime', 'exitTime'], res.list)
        const data = this.formatJson(this.headers, list)
        const { combined } = combinedFn(Object.values(this.headers).slice(1), list, ['quantity'])
        data.push(['合计', ...Object.values(combined)])
        excel.export_json_to_excel({
          header: Object.keys(this.headers),
          data,
          filename: '设备管理-特种设备统计表-' + this.orgName,
          autoWidth: true,
          bookType: 'xlsx',
          multiHeader,
          merges,
        })
      })
    },
    formatJson (headers, list) {
      return list.map((item) => {
        return Object.values(headers).map((value) => {
          return item[value]
        })
      })
    },
  },
}
</script>
<style lang="scss">

</style>
